<?php
namespace newshareTrait\Api;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

use Gy_Library\CusSession;

/**
 * Description of DataProcessor
 *
 * @author hardy
 */
trait DataProcessor{
    /**
     * 获得session ID -》 学生 id
     * @return mixed
     */
    public function getSessionId(){
        $user = CusSession::get(C('AUTH_ID'));
        return $user['id'];
    }

    /**
     * 成功返回分页数据
     * @param array $data  部分数据的列表
     * @param int $count   数据的总数据
     * @param int $page_number 分页数
     * @param string $message  提示信息
     */
    public function setSuccessMessage($data=[], $count=0,$page_number=0, $message='成功'){
        //总数
        $jsonData['count'] = $count;
        //分页数
        $jsonData['page_number'] = $page_number;
        if($count%$page_number == 0){
            $jsonData['page'] = (int)($count/$page_number);
        }else{
            $jsonData['page'] = (int)($count/$page_number)+1;
        }
        //当前记录总数
        $jsonData['number'] = count($data);
        //当前记录列表
        if(empty($data)){
            $jsonData['list'] = [];
        }else{
            $jsonData['list'] = $data;
        }
        $this->response($message, 1, $jsonData);
    }

    /**
     * 反射
     * @param $class
     * @param $function
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function outUnusual($class, $function){
        $className = new \ReflectionClass($class);
        if(!method_exists($className->name, $function)){
            E($className->name.'类中'.$function.'方法不存在');
        }
    }
    /**
     * 成功，返回一条信息记录
     * @param array $data
     * @param string $message
     */
    public function setSuccess($data=[], $message='成功'){
        if(empty($data)){
            $this->response($message, 1, []);
        }else{
            $this->response($message, 1, $data);
        }
    }
    /**
     * 失败
     * @param string $message       错误信息
     * @param array $data           可带数据
     */
    public function setFail($message='请重试',$data = []){
        if(empty($data)){
            $this->response($message,0,[]);
        }else{
            $this->response($message,0,$data);
        }
    }
    /**
     * 错误
     * @param string $message
     * @param array $data
     */
    public function setError($message='访问出错了，请重试',$data = []){
        if(empty($data)){
            $this->response($message,0,[],401);
        }else{
            $this->response($message,0,$data, 401);
        }
    }
    /***************************更改*****************************/
    /**
     * 更改某个表的数据数据
     * @param $ent   更新的数据，需要在传入之前加入id，
     * @param string $mode   默认put方法
     * @param mixed|string $table
     * @throws \ReflectionException
     */
    public function updateDataSave($ent, $mode='put.',$table=CONTROLLER_NAME){

        $data = I($mode);
        $className = new \ReflectionClass($this);
        //检测是否需要过滤key
        $unwantedKey = [];
        if(method_exists($className->name, 'getSaveUnwantedKey')){
            $unwantedKey = $this->getSaveUnwantedKey();
        }
        foreach ($data as $key => $value){
            if(in_array($key, $unwantedKey, true)){
                continue ;
            }else{
                $ent[$key] = $value;
            }
        }
        $model = D($table);
        if($model->createSave($ent) === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($ent['id']);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }
    /**
     * 更新数据并返回数据
     * 不支持事务
     * @param $model
     * @param $data
     */
    public function updateResponse($model, $data){
        if($model->createSave($data) === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($data['id']);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }

    /**
     *  更新数据
     * 支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function updateData($model, $data){
        if($model->createSave($data) === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($data['id']);
            return $oneData;
        }
    }
    /******************添加******************/
    /**
     * 插入数据库
     * 支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function createAdds($model, $data){
        $r = $model->createAdd($data);
        if($r === FALSE){
            $this->setFail($model->getError());
        }
        return $r;
    }
    /**
     * 插入数据库
     * 不支持事务
     * @param $model
     * @param $data
     * @return mixed
     */
    public function createAdd($model, $data){
        $r = $model->createAdd($data);
        if($r === FALSE){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($r);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }
    /**
     * @param string $mode
     * @param mixed|string $table
     * @throws \ReflectionException
     */
    public function createAddData($mode='post.',$table=CONTROLLER_NAME){

        $data = I($mode);
        $className = new \ReflectionClass($this);
        //检测是否需要过滤key
        $unwantedKey = [];
        if(method_exists($className->name, 'getAddUnwantedKey')){
            $unwantedKey = $this->getSaveUnwantedKey();
        }
        $ent = [];
        foreach ($data as $key => $value){
            if(in_array($key, $unwantedKey, true)){
                continue ;
            }else{
                $ent[$key] = $value;
            }
        }
        if(empty($ent)){
            $this->setFail('数据不能为空！');
        }
        $model = D($table);
        $r = $model->createAdd($ent);
        if($r === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->getOne($r);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }
    /**********************删除***********************/
    /**
     * 删除一条数据
     * @param $model
     * @param $map
     * @param string $delete_function
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function deleteData($model, $map, $delete_function='deleteData'){
        $map['id'] = I('get.id');
        if(empty($map['id'])){
            $this->setFail('请选择需要删除的信息');
        }
        //检测是否存在删除方法
        $this->outUnusual($model, $delete_function);
        //删除
        $r = $model->{$delete_function}($map);
        if($r === FALSE){
            $this->setFail($model->_error);
        }else{
            $this->setSuccess();
        }
    }

    /**
     * @param $model
     * @param $map
     * @param string $delete_function
     * @return mixed
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function deletes($model, $map, $delete_function='deleteData'){
        //检测是否存在删除方法
        $this->outUnusual($model, $delete_function);
        $r = $model->{$delete_function}($map);
        if($r === FALSE){
            $this->setFail($model->_error);
        }
        return $r;
    }
    /*************************查询****************************/
    /**
     * 方法使用的 用来以分页形式返回数据列表
     * @param $model 模型
     * @param $map 条件
     * @param string $order 排序
     * @param string $function_name
     * @param string $function_count
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function responseModelPage($map, $order = 'id desc',$model=CONTROLLER_NAME,  $function_name = 'getListForPageData',$function_count='getListForCount'){
        if(is_string($model)){
            $model = D($model);
        }
        //检测是否存在方法
        $this->outUnusual($model, $function_count);
        $count = $model->{$function_count}($map);
        $per_page = C('HOME_PER_PAGE_NUM', null, false);
        if($per_page === false){
            $page = new \Gy_Library\GyPage($count);
        }
        else{
            $page = new \Gy_Library\GyPage($count, $per_page);
        }
        //检测是否存在方法
        $this->outUnusual($model, $function_name);
        $data_list = $model->{$function_name}($map, $page->nowPage, $page->listRows, $order);
        $this->setSuccessMessage($data_list,$count,$per_page,'成功');
    }
    /**
     * 方法使用的 用来以分页形式返回数据列表
     * @param $model 模型
     * @param $map 条件
     * @param string $order 排序
     * @param string $function_name
     * @param string $function_count
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function responsePage($map=[], $order = 'id desc',$model=CONTROLLER_NAME){
        if(is_string($model)){
            $model = D($model);
        }
        $count = $model->getListForCount($map);
        $per_page = C('HOME_PER_PAGE_NUM', null, false);
        if($per_page === false){
            $page = new \Gy_Library\GyPage($count);
        }
        else{
            $page = new \Gy_Library\GyPage($count, $per_page);
        }
        $data_list = $model->getListForPageData($map, $page->nowPage, $page->listRows, $order);
        $this->setSuccessMessage($data_list,$count,$per_page,'成功');
    }

    /**
     * @param $id
     * @param mixed|string $table    表名或者是模型的实例
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function responseOneData($id,$table=CONTROLLER_NAME){
        if(is_string($table)){
            $data = D($table)->getOneData($id);
        }else{
            //检测是否存在方法
            $this->outUnusual($table, 'getOneData');
            $data = $table->getOneData($id);
        }
        if(empty($data)){
            $this->setFail('未找到相关的信息');
        }else{
            $this->setSuccess($data);
        }
    }

    /**
     * @param $id    数组或者是数字，$map条件
     * @param mixed|string $table    表名或者是模型的实例
     * @return mixed
     * @throws \ReflectionException
     * @throws \Think\Exception
     */
    public function getOneData($id,$table=CONTROLLER_NAME){
        if(is_string($table)){
            $data = D($table)->getOneData($id);
        }else{
            //检测是否存在方法
            $this->outUnusual($table, 'getOneData');
            $data = $table->getOneData($id);
        }
        return $data;
    }
}
